package com.aWei.springbootinit.manager;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.aWei.springbootinit.common.ErrorCode;
import com.aWei.springbootinit.dataSource.DataSource;
import com.aWei.springbootinit.dataSource.PictureDataSource;
import com.aWei.springbootinit.dataSource.PostDataSource;
import com.aWei.springbootinit.dataSource.UserDataSource;
import com.aWei.springbootinit.exception.BusinessException;
import com.aWei.springbootinit.exception.ThrowUtils;
import com.aWei.springbootinit.model.dto.post.PostQueryRequest;
import com.aWei.springbootinit.model.dto.search.SearchRequest;
import com.aWei.springbootinit.model.dto.user.UserQueryRequest;
import com.aWei.springbootinit.model.entity.Picture;
import com.aWei.springbootinit.model.enums.SearchTypeEnum;
import com.aWei.springbootinit.model.vo.PostVO;
import com.aWei.springbootinit.model.vo.SearchVO;
import com.aWei.springbootinit.model.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.CompletableFuture;

/**
 * @author 小维哥
 * @version 1.2
 */
@Component
public class SearchFacade {

        @Resource
        private UserDataSource userDataSource;

        @Resource
        private PostDataSource postDataSource;

        @Resource
        private PictureDataSource pictureDataSource;

        //后端所有接口的统一管理方法（门面模式）
        public SearchVO searchAll(@RequestBody SearchRequest searchRequest, HttpServletRequest request) {
                String searchText = searchRequest.getSearchText();
                String type = searchRequest.getType();
                //枚举进行类别的限制
                SearchTypeEnum searchTypeEnum = SearchTypeEnum.getEnumByValue(type);
                ThrowUtils.throwIf(StringUtils.isBlank(type), ErrorCode.PARAMS_ERROR);
                long pageSize = searchRequest.getPageSize();
                long current = searchRequest.getCurrent();
                //若类别未空，查询所有数据(图片，用户，帖子)
                if (searchTypeEnum == null) {
                        CompletableFuture<Page<UserVO>> userTask = CompletableFuture.supplyAsync(() -> {
                                UserQueryRequest userQueryRequest = new UserQueryRequest();
                                userQueryRequest.setUserName(searchText);
                                Page<UserVO> userVOPage = userDataSource.doSearch(searchText, current, pageSize);
                                return userVOPage;
                        });
                        CompletableFuture<Page<PostVO>> postTask = CompletableFuture.supplyAsync(() -> {
                                PostQueryRequest postQueryRequest = new PostQueryRequest();
                                postQueryRequest.setSearchText(searchText);
                                Page<PostVO> postVOPage = postDataSource.doSearch(searchText, current, pageSize);
                                return postVOPage;
                        });

                        CompletableFuture<Page<Picture>> pictureTask = CompletableFuture.supplyAsync(() -> {
                                Page<Picture> picturePage = pictureDataSource.doSearch(searchText, current, pageSize);
                                return picturePage;
                        });
                        CompletableFuture.allOf(userTask, postTask, pictureTask).join();
                        try {
                                Page<UserVO> userVOPage = userTask.get();
                                Page<PostVO> postVOPage = postTask.get();
                                Page<Picture> picturePage = pictureTask.get();
                                SearchVO searchVO = new SearchVO();
                                searchVO.setUserList(userVOPage.getRecords());
                                searchVO.setPostList(postVOPage.getRecords());
                                searchVO.setPictureList(picturePage.getRecords());
                                return searchVO;
                        } catch (Exception e) {
                                throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询异常");
                        }
                } else {
                        //用适配器模式进行优化
                        SearchVO searchVO = new SearchVO();
                        DataSource dataSource = null;
                        switch (searchTypeEnum) {
                                case POST:
                                        dataSource = postDataSource;
                                        break;
                                case USER:
                                        dataSource = userDataSource;
                                        break;
                                case PICTURE:
                                        dataSource = pictureDataSource;
                                        break;
                        }
                        dataSource.doSearch(searchText,current,pageSize);
                        return searchVO;
                }
        }
}
